package com.ruoyi.app.dyh.hxf.controller;


import com.ruoyi.app.dyh.hxf.domain.Active;
import com.ruoyi.app.dyh.hxf.domain.Organization;
import com.ruoyi.app.dyh.hxf.mapper.MyCreateMapper;
import com.ruoyi.app.dyh.hxf.service.IMyCreateService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("我创建的活动")
@RestController
@RequestMapping("/myCreate/team")
public class MyActiveCreateController extends BaseController {

    @Autowired
    private IMyCreateService myCreateService;

    @Autowired
    private MyCreateMapper myCreateMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询我(队长)创建的所有活动
     */
    @ApiOperation("查询我(队长)创建的所有活动")
    @GetMapping("/list")
    public AjaxResult allTeam() {
        //获取队长的Id
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<Active> allTeam = myCreateService.selectAllTeam(userId);
        return AjaxResult.success(allTeam);
    }

    /**
     * 1.查询登录者是否为服务队队长
     */
    @ApiOperation("查询登录者是否为服务队队长")
    @GetMapping
    public AjaxResult trueMyTeam() {
        String userId = SecurityUtils.getLoginUser().getUserId().toString();
        //领导Id
        List<String> str = myCreateService.selectAllId();
        for (int i = 0; i < str.size(); i++) {
            if (userId.equals(str.get(i))) {
                //如果查询到登录者的Id在里面 代表我已经是一个队伍的队长了,
                // 不能在申请,直接进入我创建的页面
                return AjaxResult.success(1000);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 服务队队长创建活动
     */
    @ApiOperation("服务队队长创建活动")
    @PostMapping
    public AjaxResult add(Active active) {
        StringBuffer sysUserSb = new StringBuffer();
        long userId = SecurityUtils.getLoginUser().getUserId();
        //从其他表中根据userId找到队伍的ID和我的名字
        Integer teamID = myCreateMapper.selectTeamID(userId);
        String myName = myCreateMapper.selectMyName(userId);
        active.setCreateBy(myName);
        active.setTeamID(teamID);
        active.setStatus(0);
        active.setActiveUserId(String.valueOf(userId));
        //获得活动的id方法:
        //查询user表中的活动Ids,然后判断如果是没有一个就是插入,有一个就是修改
        //插入后根据myName选择出活动id,
        int p = myCreateService.insertTeam(active);
        String activeId = myCreateMapper.selectActiveId(active.getActiveTitle());
        SysUser sysUser = userService.selectUserById(userId);
        int q = 0;
        int r = 0;
        if (sysUser.getActiveIds() == null) {
            sysUser.setActiveIds(activeId);
            q = userService.updateUser(sysUser);
        } else {
            String[] UserActiveIds = sysUser.getActiveIds().split(",");
            System.out.println(UserActiveIds);
            for (int i = 0; i < UserActiveIds.length; i++) {
                sysUserSb.append(UserActiveIds[i] + ",");
            }
            sysUserSb.append(activeId);
            sysUser.setActiveIds(sysUserSb.substring(0, sysUserSb.length()));
            r = userService.updateUser(sysUser);
        }
        if (p == 1 && q == 1) {
            return toAjax(1);
        } else if (p == 1 && r == 1) {
            return toAjax(1);
        } else {
            return toAjax(0);
        }
    }

    /**
     * 非服务队队长申请成为服务队队长
     */
    /**
     * 非服务队队长申请成为服务队队长
     */
    @ApiOperation("非服务队队长申请成为服务队队长")
    @PostMapping("/apply")
    public AjaxResult add(Organization organization) {
        StringBuffer sysUserSb = new StringBuffer();
        long userId = SecurityUtils.getLoginUser().getUserId();
        organization.setLeaderId(userId);
        organization.setIsSet(1);
        String myName = myCreateMapper.selectMyName(userId);
        organization.setCreateBy(myName);
        //创建组织
        int p = myCreateService.insertOrganization(organization);
        //把组织ID插入到User表中
        long teamID = myCreateMapper.selectTeamIDinsertUser(organization.getTeamName());
        //根据id查询User信息
        SysUser sysUser = userService.selectUserById(userId);
        int q = 0;
        int r = 0;
        if (sysUser.getTeamId() == null) {
            sysUser.setActiveIds(String.valueOf(teamID));
            q = userService.updateUser(sysUser);
        } else {
            String[] UserTeamIds = sysUser.getTeamId().split(",");
            System.out.println(UserTeamIds);
            for (int i = 0; i < UserTeamIds.length; i++) {
                sysUserSb.append(UserTeamIds[i] + ",");
            }
            sysUserSb.append(teamID);
            sysUser.setTeamId(sysUserSb.substring(0, sysUserSb.length()));
            r = userService.updateUser(sysUser);
        }
        if (p == 1 && q == 1) {
            return toAjax(1);
        } else if (p == 1 && r == 1) {
            return toAjax(1);
        } else {
            return toAjax(0);
        }
    }

    /**
     * 已申请服务队的查看申请信息
     * 未使用到
     */
    @ApiOperation("已申请服务队的查看申请信息")
    @GetMapping(value = "/{teamId}")
    public AjaxResult getInfo(@PathVariable("teamId") String teamId) {
        return AjaxResult.success(myCreateService.getInfo(teamId));
    }

    /**
     * 已申请服务队的修改申请信息
     * 未使用到
     */
    @ApiOperation("已申请服务队的修改申请信息")
    @PutMapping
    public AjaxResult edit(Organization organization) {
        return AjaxResult.success(myCreateService.edit(organization));
    }
}
